<template>
  <div class="login">
    <van-nav-bar
      class="nav-bar"
      title="账号登录"
      left-arrow
      @click-left="onClickLeft"
    />

    <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[
    { required: true, message: '请填写用户名' },
    {pattern: /^\w{5,8}$/,message: '宝贝,用户名为3~8位哦~~~'}
    ]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[
    { required: true, message: '请填写密码' },
    {pattern: /^\w{6,}$/,message: '宝贝,密码至少为6位哦~~~'}
    ]"
  />
  <div style="margin: 16px;">
    <van-button  block type="info" native-type="submit" color="#ffc0cb">登录</van-button>
  </div>
</van-form>
<router-link class="link" to="/register">没有账号,点我去注册</router-link>
 <div class="picture">
      <img
        src="https://img2.baidu.com/it/u=545061015,3288863041&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=707"
        alt=""
      />
  </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  methods: {
    onClickLeft () {
      this.$router.back()
    },

    async onSubmit (values) {
      const res = await login(values)
      console.log(res)
      setToken(res.body.token)
      this.$toast.success('登录成功')
      setTimeout(() => {
        this.$router.push('/')
      }, 1000)
    }

  },
  data () {
    return {
      username: '',
      password: ''
    }
  }
}
</script>

<style>
body {
  background-color: #fff;
}
.nav-bar{
  margin-bottom: 20px;
}

.link {
  display: block;
  text-align: center;
  font-size: 14px;
  color: #999;
}
.picture {
  width: 300px;
  height: 500px;
}
img {
  width: 100%;
}
</style>
